﻿#include "managerinfo.h"
#include "ui_managerinfo.h"

ManagerInfo::ManagerInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ManagerInfo)
{
    ui->setupUi(this);
    ManagerInfoInit();

    ManagerTimer = new QTimer(this);
    connect(ManagerTimer,&QTimer::timeout,this,&ManagerInfo::ON_ManagerTimer);
    ManagerTimer->start(1200);

    //获取当前日期
    ui->Selete_DateEdit->setDateTime(QDateTime::fromString(QDateTime::currentDateTime().toString("yyyy-MM-dd")+" 00:00:00" ,"yyyy-MM-dd hh:mm:ss"));
    ui->Selete_DateEdit_2->setDateTime(QDateTime::fromString(QDateTime::currentDateTime().toString("yyyy-MM-dd")+" 23:59:00" ,"yyyy-MM-dd hh:mm:ss"));

    // tableWidget 初始化
    table_single::GetInstance()->TableWidgetInit(ui->tableWidget,12,"QHeaderView::section{background-color:rgb(211, 215, 212);font:12pt '宋体';color: black;};",30,30);
    connect(ui->tableWidget,&QTableWidget::cellClicked,this,&ManagerInfo::SlotsetCurrentItem);

    //隐藏不必要的组件
    ui->widget->hide();

    ui->label_36->hide();
    ui->lineEdit->hide();

    // 隐藏库位 历史查询界面
    ui->tabWidget->setTabEnabled(1, false);
}

ManagerInfo::~ManagerInfo()
{
    delete ui;
}

void ManagerInfo::tableWidget_Show()
{
    //解决显示卡顿问题:  https://blog.csdn.net/u014746838/article/details/102642083
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    QStringList comBoxlist;
    QList<RW_Excel> ExcelList=TableWidget();
    table_single::GetInstance()->UP_TableWidget(ui->tableWidget,ExcelList,comBoxlist,0);
    if(ExcelList.isEmpty()){
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//列宽根据表格大小自适应
        return;
    }
    /**
     * 赖俊龙
     * 设置历史任务界面表格显示自适应
     */
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//列宽根据表格大小自适应
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0,QHeaderView::ResizeToContents);

}

QList<RW_Excel> ManagerInfo::TableWidget()
{
    QList<RW_Excel> ExcelList;
    QList<RW_Excel> ExcelList0;

    foreach(StationStatusUpdate iter,StationStatusUpdateMap.values())
    {
        int incount=0;
        RW_Excel RW_ExcelItem;
        RW_ExcelItem.ExcelB=QString::number(iter.ID);                                                           // 序列编码
        RW_ExcelItem.ExcelC=iter.StationName;                                                                   // 库位名称
        RW_ExcelItem.ExcelD=iter.StationNameDesc;                                                               // 库位描述
        RW_ExcelItem.ExcelE=QString::number(iter.StationStatus);                                                // 库位状态
        RW_ExcelItem.ExcelF=iter.StationStatusDesc;                                                             // 状态描述
        RW_ExcelItem.ExcelG=QString::number(iter.QRcode);                                                       // 二维码值
        RW_ExcelItem.ExcelH=iter.UpdateTime.toString("yyyy-MM-dd hh:mm:ss");                                    // 更新时间
        RW_ExcelItem.checked=iter.checked;                                                                      // 选中状态

        if(incount==0){
            incount=1;
            RW_ExcelItem.corlor=0;       // 白色
            ExcelList.append(RW_ExcelItem);
        }

    }

    for (int i=0;i<ExcelList.size();i++) {
        ExcelList[i].ExcelA=QString::number(i+1);
    }
    ui->lineEdit_count->setText(tr("项数：")+QString::number(ExcelList.size()));
    return ExcelList;
}

void ManagerInfo::ON_Translate()
{


    qApp->installTranslator(&ESS_Single::GetInstance()->trans);
    ui->retranslateUi(this);

    int _Index = ui->tabWidget->currentIndex();
    ui->tabWidget->insertTab(0,taskmanagerItem,tr("任务管理"));
    ui->tabWidget->setCurrentIndex(_Index);

    // 判断表格是否有数据
    if (ui->tableWidget->rowCount() > 0) {
        //查询按钮槽函数
        on_button_query_clicked();
    }

    //主界面翻译信号
    emit sig_Translate();
}


void ManagerInfo::ON_ManagerTimer()
{
    if(ServerInitItem.UserInfoList.isEmpty()){
        /************** 基础信息赋值 *******************/
        allServerInit ALLServerInitItem=queryServer::getInstance()->getCurrentServerInitItem();
        ServerInitItem=ALLServerInitItem.ServerInitItem;
    }
}

void ManagerInfo::ManagerInfoInit()
{
    threadQueryI=new threadQuery;
    threadQueryItem=new QThread();
    threadQueryI->moveToThread(threadQueryItem);
    threadQueryItem->start();

    wrexcelprocessItiem=new wrexcelprocess();
    wrexcelprocessItiem->hide();

    taskmanagerItem=new taskManager;
    ui->tabWidget->insertTab(0,taskmanagerItem,tr("任务管理"));
    ui->tabWidget->setCurrentIndex(0);
    connect(this,&ManagerInfo::sig_Translate,taskmanagerItem,&taskManager::ON_Translate);//主界面翻译信号

//    QLabel *label = new QLabel;
//    ui->tabWidget->setCornerWidget(label,Qt::TopRightCorner);
//    label->setText(tr("系统数据管理界面"));

//    Actionbtns<<ui->stop_Button<<ui->clear_Button;
//    for(int i=0;i<Actionbtns.length();i++){
//        connect(Actionbtns[i],&QPushButton::pressed,this,&ManagerInfo::ON_Pressed);
//        connect(Actionbtns[i],&QPushButton::released,this,&ManagerInfo::ON_Released);
//    }


}

void ManagerInfo::SlotsetCurrentItem(int row, int column)
{

    int _ID=ui->tableWidget->item(row,1)->text().trimmed().toInt();

    QMap<int, StationStatusUpdate>::iterator iter= StationStatusUpdateMap.begin();
    while (iter !=StationStatusUpdateMap.end()) {
        if(iter.value().ID==_ID){
            if(iter.value().checked==0){//选中
                iter.value().checked=1;
            }else {
                iter.value().checked=0;
            }
        }else{
            iter.value().checked=0;
        }
        iter++;
    }
    tableWidget_Show();//tableWidget 更新显示
}

void ManagerInfo::on_button_query_clicked()
{
    //清空容器
    StationStatusUpdateMap.clear();

    //后台运行状态
    this->setCursor(Qt::BusyCursor);

    //查询按钮不使能
    ui->button_query->setEnabled(false);

    //任务号
    QString scanCodeI = ui->lineEdit->text();

    //起始时间
    QDateTime QDateTimeS=ui->Selete_DateEdit->dateTime();

    //截止时间
    QDateTime QDateTimeE=ui->Selete_DateEdit_2->dateTime();

    //查询更新库位状态历史信息
    StationStatusUpdateMap = threadQueryI->select_StationStatusUpdateMap(scanCodeI, QDateTimeS, QDateTimeE);

    //tableWidget 更新显示
    tableWidget_Show();

    //查询按钮使能
    ui->button_query->setEnabled(true);

    //正常状态
    this->setCursor(Qt::ArrowCursor);
}

void ManagerInfo::on_Cancel_Button_clicked()
{
    StationStatusUpdate StationStatusUpdateI;
//    seletSAPExcelInfoI=StationStatusUpdateI;
    StationStatusUpdateMap.clear();
    tableWidget_Show();//tableWidget 更新显示

    //清空工单条码标签
    ui->lineEdit->clear();
}
